<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>利用Object.defineProperty做数据响应式</title>
  <style>
     body{
       width:100%;
       height:100%;
     }
     .box{
       width: 500px;
       height: 100px;
       overflow-y: scroll;
    
       
     }
     .red{
       width:60%;
       height:400px;
       
       background: red;
     }

  </style>
</head>
<body id="a">
  <div class="box" id="box">
    <div class="red" id="red" >
    </div>
    111111
  </div>
  
  
  
</body>
<script src="https://unpkg.com/better-scroll/dist/bscroll.min.js"></script>

<script>
  
  
  var a = [0];
  a.length = 10;
  console.log(a);
  function walk(obj){
    if(typeof obj !== 'object' || obj === null) return;
    var keys = Object.keys(obj);
    let oldVal;
    for(let i = 0;i < keys.length; i++){
       oldVal = obj[keys[i]];
       Object.defineProperty(obj,keys[i],{
         get (){
           console.log('oldVal='+ oldVal);
           return oldVal;
         },
         set(newVal){         
           console.log('newVal='+ newVal)
           if(newVal !== oldVal){
             return newVal;
           }
         }
       })
    }
  }

  walk(a);


</script>

</html>